package com.atguigu.serviceedu.controller;


import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.feign.ServiceOssFeignClient;
import com.atguigu.serviceedu.service.EduChapterService;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.atguigu.utils.TeacherQuery;
import com.atguigu.vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-06-18
 */
@RestController
@RequestMapping("/serviceedu/eduteacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private ServiceOssFeignClient serviceOssFeignClient;

    @GetMapping("/findallteacher")
    public ResultVO findAllTeacher() {
        List<EduTeacher> list = eduTeacherService.list(null);
        return ResultVO.ok().Msg("查询全部老师成功").Data("teacherList", list);
    }

    @DeleteMapping("/removeteacher/{id}")
    public ResultVO removeteacherbyid(@PathVariable("id") String id) {
        //下面的代码目的是到阿里云OSS上删除该讲师对应的头像图片,首先你先到数据库里拿到该id对应的teacher的avatarUrl
        boolean b = eduTeacherService.deleteTeacherAvatarByTeacherid(id);
        boolean isremove = eduTeacherService.removeById(id);
        if (isremove) {
            return ResultVO.ok().Msg("删除讲师成功").Data("flag", isremove);//这种写法就是链式调用，这条链上的每个节点都是为返回到前端
            //的对象属性进行赋值
        } else {
            return ResultVO.error().Msg("删除讲师失败").Data("flag", isremove);
        }
    }

    @GetMapping("/listpageteacher/{currentpage}/{pagesize}")
    public ResultVO listPageTeacher(@PathVariable("currentpage") Long currentpage, @PathVariable("pagesize") Long pagesize) {
        Page<EduTeacher> pageParm = new Page<>(currentpage, pagesize);
        IPage<EduTeacher> pageModel = eduTeacherService.page(pageParm, null);//这个对象就封装了很多你需要的信息
        //我觉得pageModel应该是pageParm同类型的对象，只不过，pageModel剔除了pageParm在querywrapper条件下的记录。
        long total = pageModel.getTotal();//就是这张表里面所有的老师数量
        List<EduTeacher> teacherList = pageModel.getRecords();//当前显示条数的情况下，当前页所有的老师记录，注意这里是当前页
        return ResultVO.ok().Msg("老师分页成功").Data("total", total).Data("rows", teacherList);
    }

    @PostMapping("/listconditionteacher/{currentpage}/{pagesize}")//这个方法就是带分页的条件查询
    public ResultVO listconditionteacher(@PathVariable long currentpage,
                                         @PathVariable long pagesize,
                                         @RequestBody(required = false) TeacherQuery teacherQuery) {
//        Long icurrentpage=Long.valueOf(currentpage);
//        Long ipagesize=Long.valueOf(pagesize);
        Page<EduTeacher> pageParm = new Page<>(currentpage, pagesize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            wrapper.le("gmt_create",end);
        }
        IPage<EduTeacher> pageModel = eduTeacherService.page(pageParm, wrapper);//这个对象就封装了很多你需要的信息
        long total = pageModel.getTotal();//就是这张表里面所有的老师数量
        List<EduTeacher> teacherList = pageModel.getRecords();//当前显示条数的情况下，当前页所有的老师记录，注意这里是当前页
        return ResultVO.ok().Msg("老师分页成功").Data("total", total).Data("rows", teacherList);
    }
    @PostMapping("/addteacher")
    public ResultVO addteacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return ResultVO.ok().Msg("添加讲师成功").Data("flag",save);
        }else {
            return  ResultVO.error().Msg("添加讲师失败").Data("flag",save);
        }
    }
    @GetMapping("/getteacherbyid/{id}")
    public ResultVO getTeacherById(@PathVariable ("id")String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return ResultVO.ok().Msg("查询老师成功").Data("teacher",teacher);
    }
    @PostMapping("/updateteacher")//注意这个接口是根据id来更新讲师的
    public ResultVO updateteacher(@RequestBody EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        if (update){
            return ResultVO.ok().Msg("更新老师成功").Data("flag",update);
        }else {
            return ResultVO.error().Msg("更新老师失败").Data("flag",update);
        }
    }
    @DeleteMapping("/removebatchteacher")
    public ResultVO removebatchteacher(@RequestBody List<String>teacheridList) {
        boolean isremove = eduTeacherService.removeByIds(teacheridList);
        if (isremove) {
            return ResultVO.ok().Msg("批量删除讲师成功").Data("flag", isremove);//这种写法就是链式调用，这条链上的每个节点都是为返回到前端
            //的对象属性进行赋值
        } else {
            return ResultVO.error().Msg("批量删除讲师失败").Data("flag", isremove);
        }
    }
    @GetMapping("/getteachernamebykeyword/{keyword}")
    public ResultVO getteachernamebykeyword(@PathVariable String keyword){
        List<Map<String, Object>> list= eduTeacherService.queryteachernamebykeyword(keyword);
        return ResultVO.ok().Msg("模糊查询成功").Data("teachernames",list);
    }
}
